﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;
using CommonHelp;
using Core.Unity;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Services;
using ViewModels;

namespace Commands {
    /// <summary>
    /// 雷达图
    /// </summary>
    public class QueryRadarCrefCommand : CommandBase {
        private IXContainer _hook;
        private IDisplayTable _IDisplayTable;

        public override void OnCreate(object hook) {
            base.OnCreate(hook);
            _hook = hook as IXContainer;

            _IDisplayTable = _hook.Resolve<IDisplayTable>();
        }

        public override void OnClick(object parameter) {
            base.OnClick(parameter);
            HttpHelper http = new HttpHelper("http://typhoon.weather.com.cn/data/radar_cref_webMercator/CR.xml");
            http.GetAsync(AfterGetData);

            this.IsEnabled = false;
        }

        private void AfterGetData(string responseStr) {
            //try {
            //    XmlDocument doc = new XmlDocument();
            //    doc.LoadXml(responseStr);

            //    List<RadarCrefInfo> infos = new List<RadarCrefInfo>();
            //    for (int i = 0; i < doc.ChildNodes.Count; i++) {
            //        var node = doc.ChildNodes[i];
            //        if (node.NodeType == XmlNodeType.XmlDeclaration || node.NodeType == XmlNodeType.Comment) {
            //            continue;
            //        }
            //        foreach (XmlNode item in node.ChildNodes) {
            //            string jsonStr = JsonConvert.SerializeXmlNode(item, Newtonsoft.Json.Formatting.None, true);
            //            jsonStr = jsonStr.Replace("\"@", "\"");
            //            RadarCrefInfo info = JsonConvert.DeserializeObject<RadarCrefInfo>(jsonStr);
            //            info.ProType = node.Attributes["proType"].Value;
            //            infos.Add(info);
            //        }
            //    }
            //    _IDisplayTable.DisplayData = infos.ToArray();
            //    _IDisplayTable.AddRowVisitor = new AddRadarCrefInfo(_hook.Resolve<DbHelp.DbHelper>());
            //    _hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + infos.Count.ToString() + " 条 雷达图 数据。");
            //}
            //catch (Exception ex) {
            //    CommonHelp.LogHelper.Error(ex);
            //}
            //finally {
            //    this.IsEnabled = true;
            //    //_hook.Resolve<WebViewModel>().Url = "http://typhoon.weather.com.cn/";

            //}
        }
    }
}
